$(function () {

    var $username = $("#username_input");

    $username.change(function () {
        var username = $username.val().trim();
        if (username.length) {
            // 预校验  将用户发送给服务器校验 使用ajax
            $.getJSON('/axf/checkuser/', {'username': username}, function (data) {
                const $username_info = $("#username_info");
                if (data['status'] === 200) {
                    $username_info.html(data['msg']).css("color",'green')
                } else if (data['status'] === 901) {
                    $username_info.html(data['msg']).css("color",'red')
                }
            });
        }
    });

    var $email_input = $("#email_input");
    $email_input.change(function () {
        var email_input = $email_input.val().trim();
        if (email_input.length){
            $.getJSON('/axf/checkmail/',{'email':email_input},function (data) {
                const $email_info = $("#email_info");
                if (data['status'] === 200){
                    $email_info.html(data['msg']).css("color",'green')
                } else if (data['status'] === 901){
                    $email_info.html(data['msg']).css("color",'red')
                }
            })
        }
    });


    $("#password_confirm").change(function () {

        var password = $("#password").val();
        var password_confirm = $(this).val();

        if (password === password_confirm) {
            $("#password_confirm_info").html("两次一致").css("color", "green");
        } else {
            $("#password_confirm_info").html("两次输入不一致").css("color", "red");
        }
    })


});

//通过检查页面中用户名提示的颜色，来控制提交按钮是否可以点击
function check() {

    const $username = $("#username_input");
    const username = $username.val().trim();
    const $username_info = $("#username_info");
    const email = $("#email_input").val().trim();
    const $email_info = $("#email_info");
    if (!username){
        $username_info.html("请输入用户名！").css("color",'red');
        return false;
    }
    if (!email){
        $email_info.html("请输入邮箱！").css("color",'red');
        return false;
    }

    //如果有红色提示，则不允许提交
    const username_color = $username_info.css("color");
    const email_color = $email_info.css("color");

    if (username_color === "rgb(255, 0, 0)" || email_color === "rgb(255, 0, 0)") {
        return false;
    }

    var $password_input = $("#password_input");
    var password = $password_input.val().trim();
    $password_input.val(md5(password));

    return true

}
